//
//  MainViewController.swift
//  cxd4iphone
//
//  Created by hexy on 15/11/19.
//  Copyright © 2015年 hexy. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController, UITabBarControllerDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        delegate = self
        
        setAppearance()
        
        addChildViewControllers()
    }
    
    
    private func setAppearance() {
        
        // 1 > 设置NavigationBar的外观
        let appearance = UINavigationBar.appearance()
        
        appearance.translucent = false
        appearance.barTintColor = xx_colorWithHex(hexValue: 0x3e64b5)
//        appearance.barTintColor = xx_colorWithHex(hexValue: 0x054ce4)
        appearance.tintColor = UIColor.whiteColor()
        appearance.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor(), NSFontAttributeName: xx_fontOfSize(size: 18)];
        
        // 2 > 设置tabBar外观
        tabBar.tintColor = xx_colorWithHex(hexValue: 0x3d64b4)
        
    }
    
    ///  添加所有的子控制器
    private func addChildViewControllers() {
        
        addChildViewController(ChoiceViewController(), title: "精选", norImage: UIImage(named: "tabBar_choice_normal"), selectedImage: UIImage(named: "tabBar_choice_selected"))
        
        addChildViewController(InvestViewController(), title: "投资", norImage: UIImage(named: "tabBar_invest_normal"), selectedImage: UIImage(named: "tabBar_invest_selected"))
        
        addChildViewController(ServiceViewController(), title: "服务", norImage: UIImage(named: "tabBar_service_normal"), selectedImage: UIImage(named: "tabBar_service_selected"))
        
        addChildViewController(me, title: "我", norImage: UIImage(named: "tabBar_me_normal"), selectedImage: UIImage(named: "tabBar_me_selected"))
        
    }
    
    ///  添加一个子控制器
    private func addChildViewController(vc: UIViewController, title: String, norImage: UIImage?,selectedImage: UIImage?) {
        
        vc.title = title
        vc.tabBarItem.image = norImage
        vc.tabBarItem.selectedImage = selectedImage
        
        let nav = NavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }
    
    lazy var me: UIViewController = MeViewController()
    
}


extension MainViewController {
    
    func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
        
        if let top = (viewController as! NavigationController).topViewController {
            
            if top == me {
                
                return shouldSelectViewController()
            }
        }
        
        return true
    }
    
    func shouldSelectViewController() -> Bool {
        
        if AccountViewModel.shared.userLogin {
            
            if AccountViewModel.shared.needGesture {
                
                presentGesture()
                return false
                
            } else {
                
                return true
            }
            
        } else {
            
            presentLogin()
            return false
        }
    }
    
    func presentLogin() {
        
        let root = (UIApplication.sharedApplication().keyWindow?.rootViewController)!
        let nav = NavigationController(rootViewController: LoginViewController(aTitle: "登录"))
        root.presentViewController(nav, animated: true, completion: {})
    }
    
    func presentGesture() {
        
        let root = (UIApplication.sharedApplication().keyWindow?.rootViewController)!
        
        let gvc = GestureViewController(aTitle: "手势解锁")
        gvc.type = GestureViewControllerTypeLogin
        
        root.presentViewController(gvc, animated: true, completion: {})
    }
}